package com.freeseed.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.freeseed.model.entity.Major;
import com.freeseed.model.mapper.MajorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.object.UpdatableSqlQuery;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Allwayz
 * @since 2020-03-20
 */
@Slf4j
@RestController
@RequestMapping("majors")
public class MajorController {
    @Autowired
    private MajorMapper majorMapper;

    @GetMapping
    public List<Major> getMajorList(){
        List<Major> majorList = majorMapper.selectList(new QueryWrapper<>());
        return majorList;
    }

    @GetMapping("{Id}")
    public Major getMajorById(@PathVariable int Id){
        Major major = majorMapper.selectById(Id);
        return major;
    }

    @PostMapping
    public Major addMajor(Major major){
        majorMapper.insert(major);
        return major;
    }

    @PutMapping
    public Major updateMajor(Major major){
        majorMapper.update(major,new UpdateWrapper<Major>().eq("major_id",major.getMajorId()));
        return major;
    }

    @DeleteMapping("{Id}")
    public void deleteMajorById(@PathVariable int Id){
        majorMapper.deleteById(Id);
    }
}
